package root.electronclass.electronclass.grade.homework.viewmodel

import androidx.lifecycle.*
import root.electronclass.electronclass.mvp.rest.ClassApi
import root.electronclass.electronclass.ext.CoroutineExceptionHandler
import root.electronclass.electronclass.ext.NotifyWhenChangeLiveData
import root.electronclass.electronclass.grade.model.ClassHomeworkUser
import root.electronclass.electronclass.grade.repository.ClassRepository

/**
 *
 * @author Lokiy
 * @date 2023/2/18 19:55
 */
class ClassHomeworkDetailViewModel(private val stateHandle: SavedStateHandle) : ViewModel() {

    val searchKey = NotifyWhenChangeLiveData("")
    val status = NotifyWhenChangeLiveData(0)
    val detail: LiveData<List<ClassHomeworkUser>> = searchKey.switchMap { key ->
        status.switchMap { status ->
            liveData(CoroutineExceptionHandler()) {
                emit(ClassRepository.getClassHomeworkDetail(stateHandle["id"] ?: "", key, status).records)
            }
        }
    }
    val complete: LiveData<Float> = liveData(CoroutineExceptionHandler()) {
        emit(ClassApi.service.getHomeworkFinishRate(stateHandle["id"] ?: "")?.rows ?: 0f)
    }
}